import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { visualizer } from 'rollup-plugin-visualizer';

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig((command, mode) => {
    const env = loadEnv(mode, process.cwd(), "");
    // console.log("env", env);
    return {
        plugins: [
            vue(),
            // visualizer({
            //     emitFile: false,
            //     file: "stats.html", //分析图生成的文件名
            //     open:true //如果存在本地服务端口，将在打包后自动展示
            //   }),
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
            
        ],
        resolve: {
            alias: {
                "@": fileURLToPath(new URL("./src", import.meta.url)),
            },
        },
        server: {
            proxy: {
                "/api": {
                    target: env.VITE_API_URL,
                    // target: "http://121.40.140.189",
                    changeOrigin: true,
                    // rewrite: (path) => {
                    //     console.log("path", path);
                    //     // path.replace(/^\/api/, "");
                    //     return path
                    // },
                },
                //访问localhost:6163的做拦截转发
                // "/igs": {
                //     target: "http://ezgu7t.natappfree.cc",
                //     // target: "http://localhost:6163",
                //     changeOrigin: true,
                //     rewrite: (path) => {
                //         console.log("path", path);
                //         return path;
                //     },
                // }
            },
        },
        build: {
            outDir: "dist",
            //按照文件类型拆分打包
            rollupOptions: {
                output: {
                    chunkFileNames: "assets/js/[name]-[hash].js",
                    entryFileNames: "assets/js/[name]-[hash].js",
                    assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
                },
            },
        },
        base: "/traffic-master/",
    };
})
